# encoding=UTF-8
import sys
import threading
import time

from uiautomator import Device

reload(sys)
sys.setdefaultencoding('utf-8')

devices = {
    "OPPO": ["CIF689JBOZT4L79D"],
    "HUAWEI": ['DU2SSE15C7051173', "WTKGK17214000041", "5LM0216114005932", "M9N7N15315002694"],
    "XIAOMI": ["14dc2c34", "85256580", "d0ed6633"],
    "VIVO": ["25964aff"],
    "SAMSUNG": ["3343da60", "9bf6c0eb"],
    "HTC": [],
    "MEIZU": ["98AKBNN22H38"],
}

class UiAuto(threading.Thread):
    def __init__(self, devId, port):
        threading.Thread.__init__(self)
        self.deviceId = devId
        self.stopFlag = False
        self.alive = False
        self.d = Device(devId, local_port=port, adb_server_host='127.0.0.1')

    def stop(self):
        self.stopFlag = True

    def watcherTriggered(self, wtName):
        devId = self.d.server.adb.default_serial
        if self.d.watcher(wtName).triggered:
            print wtName, "on ", devId, " is triggered"

    def registerWatcher(self):
        # 所有设备都需要监控的watcher
        self.d.watcher("monkey_1").when(textContains="悬浮球点击没反应").click(text="好的")
        self.d.watcher("monkey_2").when(textContains="更稳定流畅的语音体验，请开启如下权限").click(text="我知道了")
        self.d.watcher("monkey_3").when(textContains="悬浮球+表情换脸").click(resourceId="com.duowan.bi:id/btn_close_tips_card")
        self.d.watcher("remeber_position_w2").when(text="读取位置信息").click(text="允许")
        self.d.watcher("allow_record").when(text="启动录音").click(text="允许")


        devId = self.d.server.adb.default_serial
        if devId in devices["OPPO"]:
            print "register oppo watchers on device:", devId
            self.d.watcher("oppo_no_more_remind").when(text="不再提醒").click(text="不再提醒")
            self.d.watcher("oppo_safeTip_yes").when(text="安全提醒").click(text="同意并继续")
            self.d.watcher("oppo_safeok_yes").when(text="安装完成").click(text="完成")
            self.d.watcher("oppo_installnewversion_yes").when(text="继续安装旧版本").click(text="继续安装旧版本")
            self.d.watcher("oppo_still_install_yes").when(text="继续安装").click(text="继续安装")
            self.d.watcher("oppo_install_replace").when(textContains="替换当前版本").click(text="替换")
            self.d.watcher("oppo_allow_recording").when(textContains="YY正在尝试录音或通话录音").click(text="同意并继续")
            self.d.watcher("oppo_allow_camera").when(textContains="YY正在尝试使用摄像头").click(text="同意并继续")
            self.d.watcher("oppo_allow_install").when(textContains="静默安装拦截").click(text="允许")
            self.d.watcher("oppo_delete").when(textContains="删除卸载残留").click(text="删除")
            self.d.watcher("oppo_allow_gps").when(textContains="GPS").click(text="允许")
        elif devId in devices["HUAWEI"]:
            print "register huawei watchers on device:", devId
            self.d.watcher("huawei_install_delete_w1").when(text="暂不删除").click(text="立即删除")
            self.d.watcher("huawei_member_permit_w2").when(text="记住我的选择").click(text="允许")
            self.d.watcher("huawei_install_continue_w13").when(text="我已充分了解该风险，继续安装").click(text="我已充分了解该风险，继续安装")
            self.d.watcher("huawei_install_continue_w14").when(text="该应用版本较低，无法体验最新功能，建议安装官方最新版本。").click(text="继续安装")
            self.d.watcher("huawei_install_continue_meta8").when(text="风险提示").click(text="继续安装")
            self.d.watcher("huawei_install_allow").when(text="读取已安装应用列表").click(text="允许")
        elif devId in devices["VIVO"]:
            print "register vivo watchers on device:", devId
            self.d.watcher("vivo_install").when(text="安装").click(text="安装")
            self.d.watcher("vivo_secure_ok_w14").when(text="安全警告").click(text="好")
            self.d.watcher("vivo_install_yes_w1").when(text="来源于非官方商店的应用可能对您的手机和数据安全造成威胁，您要安装此应用吗？").click(text="安装")
            self.d.watcher("vivo_secure_yes_w2").when(text="权限请求").click(text="允许")
        elif devId in devices["XIAOMI"]:
            print "register xiaomi watchers on device:", devId
            self.d.watcher("mi4_install_continue_w2").when(textContains="继续安装").click(text="继续安装")
            self.d.watcher("mi4_lanjie_permit_w1").when(text="静默安装拦截").click(text="允许")
            self.d.watcher("mi4_locate_permit").when(text="安全警告").click(text="允许")
            self.d.watcher("mi_uninstall_rubbish").when(textContains="残留垃圾").click(text="忽略")
        elif devId in devices["SAMSUNG"]:
            print "register samsung watchers on device:", devId
            self.d.watcher("samsung_attention_sure_w1").when(text="注意").click(text="确定")
            self.d.watcher("samsung_admin_sure_w2").when(text="应用程序权限管理").click(text="确认")
        elif devId in devices["HTC"]:
            print "register HTC watchers on device:", devId
            self.d.watcher("HTC_appOp_sure_w1").when(text="应用程序操作").click(text="确定")
        elif devId in devices["MEIZU"]:
            print "register MEIZU watchers on device:", devId
            self.d.watcher("MEIZU_allow_install").when(text="正在通过USB自动安装以下应用").click(text="允许")
            self.d.watcher("MEIZU_allow_locate").when(textContains="申请获取定位权限").click(text="允许")

        self.watches = self.d.watchers

    def run(self):
        self.registerWatcher()
        while self.stopFlag == False:
            try:
                self.d.watchers.run()
                if self.watches.triggered:
                    map(self.watcherTriggered, self.watches)
                    self.watches.reset()
            except Exception, e:
                print "JsonRPCError:", e
            time.sleep(0.5)


if __name__ == "__main__":


    t = UiAuto('d0ed6633', 5577)
    t.setDaemon(True)
    t.start()
    time.sleep(20)
    # t.stop()

    while 1:
        time.sleep(10)
        pass
